<template>
	<view style="width: 100%;">
		<view class="fileList" v-if="fileList.length" :style="{left:'-'+labelWidth,width:`calc(100% + ${labelWidth})`}">
			<u-upload width="180rpx" height="180rpx" :fileList="fileList" v-bind="$attrs" @afterRead="afterRead"
				@delete="deletePic"></u-upload>
		</view>
		<view class="item" v-else>
			<u-upload v-bind="$attrs" @afterRead="afterRead" @delete="deletePic">
				<view class="f">
					<u-icon name="plus-circle" color="#08B5FF"></u-icon>
					<text>上传</text>
				</view>
			</u-upload>
		</view>
	</view>
</template>

<script>
	import {
		base
	} from '@/utils/request/request.js'

	export default {
		name: 'smma-upload',
		props: {
			labelWidth: {
				type: [String, Number],
				default: uni.$u.props.form.labelWidth
			},
			value: {
				type: [String, Array],
				default: ''
			}
		},
		watch: {
			value: {
				immediate: true,
				handler(oldV, newV) {
					let val = this.value;
					let arr = [];
					if (val instanceof Array) {
						val.forEach(item=>{
							arr.push({
								status: 'success',
								message: '',
								saveUrl:item,
								url:this.$util.getFileUrl(item)
							})
						})
					} else {
						val.split(",").forEach(item=>{
							arr.push({
								status: 'success',
								message: '',
								saveUrl:item,
								url:this.$util.getFileUrl(item)
							})
						})
					}
					this.fileList = arr;
					console.log(this.fileList);
				}
			}
		},
		data() {
			return {
				fileList: []
			}
		},
		methods: {
			deletePic(event) {
				this.fileList.splice(event.index, 1)
			},
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				let arr = []
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						saveUrl:result,
						url: this.$util.getFileUrl(result)
					}))
					
					fileListLen++
				};
				this.fileList.forEach(item=>{
					arr.push(item.saveUrl)
				})
			
				this.$emit("input", arr.join(","))

			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: base + '/sys/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							'X-Access-Token': uni.getStorageSync('token'),
							// "X-Sign": sign,
							// "X-TIMESTAMP": signMd5Utils.getTimestamp()
						},
						formData: {},
						success: (res) => {
							let data = JSON.parse(res.data)

							if (data.success) {
								resolve(data.message)
							} else {
								uni.showToast({
									icon: "none",
									title: res.message
								})
							}
							// setTimeout(() => {
							// 	resolve(res.data.data)
							// }, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style scoped lang="less">
	.item {
		width: 100%;
		color: #08B5FF;
		position: relative;

		.f {
			display: flex;
			position: absolute;
			right: 0;
		}
	}

	.fileList {
		position: relative;
		top: 64rpx;
		padding-bottom: 64rpx;
		/deep/ .u-upload__wrap__preview {
			margin: 0 46rpx 16rpx 0;
			&:nth-child(3n) {
				margin-right: 0;
			}
		}

		/deep/ .u-upload__button {
			margin: 0 0 16rpx 0;
		}

	}
</style>